package kevin.newmybatis.mybatis.handler;

import kevin.newmybatis.mybatis.model.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


/**
 *处理Statement
 */
public class StatementHandler {





    public <T> T query(String sql, String param,Class<T> classType){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        User user = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8", "root", "root");
            String sqls = String.format(sql, Integer.parseInt(param));
            preparedStatement = connection.prepareStatement(sqls);

            return  new ResultSetHandler().getResult(preparedStatement,classType);


        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
                preparedStatement.close();
            } catch (SQLException sq) {
                sq.printStackTrace();
            }


        }
            return null;
 }




}
